using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using Rensoft.Server.Security;

public class WebsiteUser
{
    private int userId;
    private int clientId;
    private string username;
    private string password;
    private WebsiteUserType type;
    private string sid;
    private string fullName;
    private bool primary;

    public int UserId
    {
        get { return userId; }
        set { userId = value; }
    }

    public int ClientId
    {
        get { return clientId; }
        set { clientId = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    public WebsiteUserType Type
    {
        get { return type; }
        set { type = value; }
    }

    public string Sid
    {
        get { return sid; }
        set { sid = value; }
    }

    public bool Primary
    {
        get { return primary; }
        set { primary = value; }
    }

    public string FullName
    {
        get
        {
            switch (type)
            {
                case WebsiteUserType.Anonymous:
                    return Settings.Client.AnonymousUserFullName;

                case WebsiteUserType.WorkerProcess:
                    return Settings.Client.WorkerProcessUserFullName;

                default:
                    return this.fullName;
            }
        }
        set
        {
            this.fullName = value;
        }
    }

    public string Description
    {
        get
        {
            switch (type)
            {
                case WebsiteUserType.Anonymous:
                    return Settings.Client.AnonymousUserFullName;

                case WebsiteUserType.WorkerProcess:
                    return Settings.Client.WorkerProcessUserFullName;

                default:
                    return string.Format(Settings.Client.SystemUserDescriptionFormat, fullName);
            }
        }
        set { /* Intentionally do nothing. */ }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public WebsiteUser() { }

    public WebsiteUser(
        int clientId,
        string username,
        string password,
        WebsiteUserType type,
        string fullName,
        bool primary)
    {
        this.clientId = clientId;
        this.username = username;
        this.password = password;
        this.type = type;
        this.fullName = fullName;
        this.primary = primary;
    }

    public WebsiteUser(
        int userId,
        int clientId,
        string username,
        string password,
        WebsiteUserType type,
        string fullName,
        bool primary,
        string sid)
        : this(clientId, username, password, type, fullName, primary)
    {
        this.userId = userId;
        this.sid = sid;
    }

    /// <summary>
    /// Creates an incomplete windows user version.
    /// </summary>
    /// <returns></returns>
    public WindowsUser ToWindowsUser()
    {
        return new WindowsUser(
            username, password, null, null, (WindowsUserFlag)0);
    }

    public void Create()
    {
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.HostingDatabase))
        {
            SqlCommand command = new SqlCommand("UserCreate", connection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("clientId", clientId);
            command.Parameters.AddWithValue("username", username);
            command.Parameters.AddWithValue("password", password);
            command.Parameters.AddWithValue("fullName", fullName);
            command.Parameters.AddWithValue("sid", sid);
            command.Parameters.AddWithValue("typeFlag", type);
            command.Parameters.AddWithValue("primaryUser", primary);
            command.Parameters.Add("userId", SqlDbType.Int).Direction = ParameterDirection.Output;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            userId = (int)command.Parameters["userId"].Value;
        }
    }

    public static WebsiteUser Create(
        int clientId,
        string username,
        string password,
        string fullName,
        WebsiteUserType type,
        string sid,
        bool primary)
    {
        WebsiteUser user = new WebsiteUser(clientId, username, password, type, fullName, primary);
        user.sid = sid;
        user.Create();
        return user;
    }

    public static List<WebsiteUser> Fetch(ClientAccount client)
    {
        List<WebsiteUser> userList = new List<WebsiteUser>();
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.HostingDatabase))
        {
            SqlCommand command = new SqlCommand("UserFetchByClientId", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("clientId", client.ClientId);

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                WebsiteUser user = new WebsiteUser(
                    (int)reader["UserId"],
                    (int)reader["ClientId"],
                    (string)reader["Username"],
                    (string)reader["Password"],
                    (WebsiteUserType)reader["TypeFlag"],
                    (string)reader["FullName"],
                    (bool)reader["PrimaryUser"],
                    (string)reader["Sid"]);

                userList.Add(user);
            }

            connection.Close();
        }

        return userList;
    }
}

public enum WebsiteUserType
{
    System, Anonymous, WorkerProcess
}